<template>
    <div class="app-container">
       <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="90px">
          <el-form-item label="用户昵称：">
            <el-input
                v-model="queryParams.keyword"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item label="用户ID：">
             <el-input
                v-model="queryParams.authKeword"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item>
             <el-button icon="Refresh" @click="resetQuery">重置</el-button>
             <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
          </el-form-item>
       </el-form>
       <el-row :gutter="10" class="mb8"> 
         <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" size="mini" @click="handleAdd">新增</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
       </el-row>
       <!-- 表格数据 -->
       <el-table v-loading="loading" :data="roleList">
          <el-table-column label="" prop="id" width="100" />
          <el-table-column label="用户头像" prop="roleName"  />
          <el-table-column label="用户昵称" prop="roleSort" />
          <el-table-column label="用户ID" prop="roleKey"/>
          <el-table-column label="" align="center" class-name="small-padding fixed-width">
             <template #default="scope">
                 <el-popconfirm   
                    confirmButtonText="确定"
                    cancelButtonText="取消"
                    title="确认要撤回该条消息吗？"
                    @confirm="handleBack(scope.row)"
                 >
                    <template #reference>
                        <el-button link type="primary">撤回</el-button>
                    </template>
                 </el-popconfirm>
             </template>
          </el-table-column>
       </el-table>
 
       <pagination
          v-show="total > 0"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
       />
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
          <el-form :model="form" :rules="rules" label-width="100px">
             <el-form-item label="用户头像" prop="avatar">
                <el-upload
                    class="upload-demo"
                    :auto-upload="false"
                    :on-change="handleChange"
                    :file-list="fileList"
                    list-type="picture"
                    ref="uploadRef"
                >
                    <el-button size="small" type="primary">点击上传</el-button>
                </el-upload>
             </el-form-item>
             <el-form-item label="用户昵称" prop="userName">
                <el-input show-password v-model="form.userName" placeholder="请输入昵称" />
             </el-form-item>
             <el-form-item label="用户ID" prop="userId">
                <el-input show-password v-model="form.userId" placeholder="请输入ID" />
             </el-form-item>
          </el-form>
          <template #footer>
             <div class="dialog-footer">
                <el-button type="primary" @click="submitDataScope">确 定</el-button>
                <el-button @click="cancelDataScope">取 消</el-button>
             </div>
          </template>
       </el-dialog>
    </div>
</template>
<script setup name="userMessage">
import { ref, reactive, onMounted, onBeforeUnmount } from 'vue'
import { getAccountList } from "@/api/im/account";
 const { proxy } = getCurrentInstance();
 
 const roleList = ref([]);
 const loading = ref(true);
 const showSearch = ref(true);
 const total = ref(0);
 const title = ref("");
 const dateRange = ref([]);
 const open = ref(false);
 const fileList = ref([]);
 const uploadRef = ref();
 
 const data = reactive({
   form: {},
   queryParams: {
     keyword: undefined,
     authKeword: '',
   },
   pageObj:{
      pageNumber: 1,
      showNumber: 10
   },
   rules: {
    avatar: [{ required: true, message: "用户头像不能为空", trigger: "blur" }],
    userName: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
    userId: [{ required: true, message: "用户ID不能为空", trigger: "blur" }]    
   },
 });
 
 const { queryParams, form, rules, pageObj } = toRefs(data);
  
 /** 查询角色列表 */
 function getList() {
   loading.value = true;
   const params ={
      pagination: pageObj.value,
      keyword: queryParams.value.authKeword != '' ? queryParams.value.authKeword : queryParams.value.keyword,
   }
   getAccountList(params).then(response => {
     roleList.value = response.data.notificationAccounts;
     total.value = response.data.total;
     loading.value = false;
   });
 }
 
 /** 搜索按钮操作 */
 function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
 }
 
 /** 重置按钮操作 */
 function resetQuery() {
   dateRange.value = [];
   proxy.resetForm("queryRef");
   handleQuery();
 }
 function handleAdd(){
    title.value = "新增";
    open.value = true;
 }
 // 文件改变时处理预览
function handleChange(file, fileList) {
  // 可以在这里对文件做额外处理
  console.log('当前文件列表:', fileList)
}

// 手动上传触发
function submitUpload() {
  uploadRef.value.submit()
}
 getList();
</script>